class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            fileimgshow: !0,
            imgsrc: '',
        };
    }



    componentDidMount() {
        this.setState({showloading:'ON'})
        // let UE = window.UE,that=this
        // let ueditor = UE.getEditor('ueditor', {
        //     autoHeightEnabled: true,
        //     autoFloatEnabled: true
        // });
        // ueditor.addListener("ready", function () {
        //     that.setState({ ueditor }, () => {
        //     });
        // }, 2);
        this.freshlist()   
    }
    freshlist(){ 
        let that=this;   
        this.setState({showloading:''})

        // Welcomepagegetdetail(res => {
        //     this.setState({showloading:''})
        //     if (res.issuccess == 1) {
        //         if (res.data) {
        //             let x = res.data,{imgsrc}=that.state
        //             imgsrc = `${API_HEAD}imgs/${x.logo}`
        //             // let {ueditor}=that.state
        //             // ueditor.setContent(that.replaceraw(decodeURIComponent(x.cashrules)))
        //             that.setState({slogan:x.slogan,reminder:x.reminder,imgsrc, fileimgshow: !1, })
        //         }
        //     }
        // })
        }
        
    filechangeimg(e) {
        var that = this;
        var filedata = $('#file3')[0].files[0];
        let _name, _fileName,filedatasize=3072;
        _name = $('#file3')[0].value;
        _fileName = _name.substring(_name.lastIndexOf(".") + 1).toLowerCase();
        if (_fileName !== "png" && _fileName !== "jpg" && _fileName !== "gif" && _fileName !== "jpeg") {
            alert("上传图片格式不正确，请重新上传");
        } else {
            if(filedatasize<filedata.size/1024){
                alert("上传图片不能大于3M");
                return;
            }
            this.setState({
                fileimgshow: !1,
                imgstate: ''
            })
            var reader = new FileReader();
            reader.readAsDataURL(filedata);
            reader.onload = function (e) {
                var base64str = e.target.result;
                that.putswiperimglistdata([base64str])
            }
        }
    }
    putswiperimglistdata(target) {


        let { imgsrc } = this.state;
        imgsrc = target[0]
        if (!isBase64(imgsrc)) {
            if (imgsrc.indexOf(`${API_HEAD}`) == -1) {
                console.log(src, 'srcsrc')
                imgsrc = `${API_HEAD}imgs/${src}`
                // imgsrc = src

            }
        }
        this.setState({
            imgsrc
        })

    }
    clickupload(e) {
        $('#file' + e).click();
    }
    del_img(e) {
        console.log(e, 'eeeeeeee')
        if (e == 3) {
            this.setState({
                imgsrc: '',
                fileimgshow: !0,
                imgstate: ''
            })
        }
    }
    // 上传图片
    uploadimg(arr) {
        let uploadimgarr = [],
            { slogan,reminder,imgsrc } = this.state, that = this;
        arr.forEach((x, i) => {
            if (x.indexOf(`${API_HEAD}`) == -1) {
                if (isBase64(x)) {
                    uploadimg(x, res => {
                        if (res.issuccess == 1) {
                            x = `${API_HEAD}imgs/${res.path}`
                            uploadimgarr.push(x)
                        } else {
                            this.setState({showloading:''})
                            alert(res.msg)
                            err = 1
                            return;
                        }
                    })
                }
            } else {
                uploadimgarr.push(x)
            }
        })
        if (uploadimgarr.length == arr.length) {
            uploadimgarr.forEach((x, i) => {
                if (i == 0) {
                    imgsrc = x.replace(`${API_HEAD}imgs/`, '')
                }
                //  else {
                //     let lb = x.replace(`${API_HEAD}imgs/`, '')
                //     lbimgarr.push(lb)
                // }
            })

            var data = { slogan,reminder,logo:imgsrc };
            console.log(data,'data');
            // if (this.state.id > 0) {
            //     data['id'] = this.state.id;
            // }
            // console.log(data, 'data');

            Welcomepageedit(data, res => {
                that.setState({showloading:''})
                if (res.issuccess == 1) {
                    // that.setState({ imgsrc: '', fileimgshow: !0, imgstate: '', id: 0, to: '' })
                    alert("保存成功");
                    // that.freshlist()
                } else {
                    alert(res.msg)
                }
            })

        }

    }
    inputchange(prop, e) {
        this.setState({ [prop]: e.target.value })

    }
    submit() {
        var { slogan,reminder,imgsrc } = this.state, that = this, err = '', imgarr = [];
        if (reminder=='') err = '请输入提示语';
        if (slogan=='') err = "请输入宣传语"
        if (!imgsrc) err = "请添加logo"
        if (err.length > 0) {
            alert(err);
            return
        }
        this.setState({showloading:'ON'})
        imgarr = imgarr.concat(imgsrc)
        this.uploadimg(imgarr)
        // let data={withdrawal:withdrawal*100,cashrules:that.replaceraw(encodeURIComponent(html)),percentage}
        // settings(data, res => {
        //     this.setState({showloading:''})
        //     if (res.issuccess == 1) {
        //         alert("保存成功");
        //     } else {
        //         alert(res.msg)
        //     }
        // })
    }
    filechangeaudio(){
        let url = $('#file')[0].files[0],that=this;
        let _name, _fileName;
        console.log(url, 'url');
        // if (audiosrc.size > 1024 * 60 * 1000) {
        //     alert('音频不能超过60M')
        //     return;
        // }

        _name = $('#file')[0].value;
        _fileName = _name.substring(_name.lastIndexOf(".") + 1).toLowerCase();
        console.log(_name, '_name_name_name');
        console.log(_fileName, '_fileName_fileName');

        if (_fileName !== "xls"&&_fileName !== "xlsx") {
            alert("上传execl格式不正确，请重新上传");
        } 
        var reader = new FileReader();
        reader.readAsBinaryString(url);
	reader.onload = function(e) {
		var data = e.target.result;
        var workbook = XLSX.read(data, {type: 'binary'});
        console.log(workbook,'workbookworkbook');
        that.outputWorkbook(workbook)
		// if(callback) callback(workbook);
    };
    }
    outputWorkbook(workbook){
        let sheetNames = workbook.SheetNames,
        worksheet = workbook.Sheets[sheetNames[0]],
        worksheetjson=XLSX.utils.sheet_to_json(worksheet),creatarr=[],rangearr=[]
        worksheetjson.forEach(x=>{
            creatarr.push(xlsxformatDate(x["日期"],'-'))
            rangearr.push(x["当日涨跌幅度"])
        })
        let creatstr=creatarr.toString(),rangestr=rangearr.toString(),data={creatstr,rangestr}
        uploadexcel(data,res=>{
            
        })
   
    }
    render() {
        let { imgsrc,slogan,reminder} = this.state;

        return (
            <div className="breadLinebox">
                <Loading hidden={this.state.showloading!='ON'}/>
                <div className='content_wrap panel-default content_wrap'>
                    <div className="breadLine">
                        <div className="img">
                            <img src='./src/lm_order1.png' />
                            <span style={{ cursor: 'pointer' }}>导入</span>
                        </div>
                    </div>
                    <div className="content form">
                       
                        <div className="formbox">
                            <div className="formcontent">
                                <div className="input-group">
                                    <span className="input-group-addon">导入</span>
                                    <input class="import-btn" onChange={this.filechangeaudio.bind(this)} type="file"  id='file' accept=".xls" />
                                </div>

                            </div>
                            <div className="submitbox">
                                <button type="button" onClick={this.submit.bind(this)} className="submitbutton">提交</button>

                            </div>
                        </div>
                    </div>
                </div>
            </div>


        )


    }
}





ReactDOM.render(
    <div>
        <TopLine />
        <div id='body'>
            <LeftMenu code='execl' />
            <Main />
        </div>
    </div>
    // <Barrage/>
    , $('.container')[0]
)